﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using eCRAM.Dao;
using eCRAM.Model;
using eCRAM.Tools;
using Microsoft.Practices.Unity;

namespace eCRAM.Bll
{
    public class UserManager 
    {
        /// <summary>
        /// Méthode de connexion d'un utilisateur
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User ConnectUser(string login, string password)
        {
            var activeDirectory = OutilsConfiguration.ActiveDirectoryController;
            try
            {
                // Tentative de connexion au serveur AD
                var ldap = new DirectoryEntry(string.Format("LDAP://{0}", activeDirectory), login, password);
                new DirectorySearcher(ldap).FindOne();
            }
            catch(DirectoryServicesCOMException e)
            {
                throw new Exception("Le compte ou le mot de passe n'est pas valide sur le serveur Active Directory", e);
            }
            catch (COMException ce)
            {
                throw new Exception(string.Format("Le controlleur Active Directory {0} n'est pas accessible", activeDirectory), ce);
            }

            var list = UserRepository.GetAll();

            //FIXME I guess the password is not encrypted...
            var ret = list.Where(c => c.Login.ToUpper().Equals(login.ToUpper())).FirstOrDefault();
            if( ret == null)
            {
                throw new Exception("L'utilisateur ne dispose pas de compte valide");
            }
            return ret;
        }

        //public User GetUserByLogin(string login)
        //{
        //    return UserRepository.GetUserByLogin(login);
        //}

        /// <summary>
        /// Récupére l'ensemble des utilisateurs
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<User> GetAll()
        {
            return UserRepository.GetAll();
        }


        /// <summary>
        /// Récupére un utilisateur à partir de son id
        /// </summary>
        /// <param name="idUser"></param>
        /// <returns></returns>
        public static User GetUserById(Guid idUser)
        {
            return UserRepository.GetById(idUser);
        }

        public static void Create(User user)
        {
            UserRepository.Create(user);
        }

        public static void Update(User user)
        {
            UserRepository.Update(user);
        }

        public static User GetUserByLogin(string login)
        {
            return UserRepository.GetUserByLogin(login);
        }

        public static void DeleteUserActivity(Guid idUserActivity)
        {
            UserRepository.DeleteUserActivity(idUserActivity);
        }


        public static void AddUserActivity(User_Activity item)
        {
            UserRepository.AddUserActivity(item);
        }
    }
}
